<div id="div_altera_grade">
<p style="font-size: 14px">Registro de Hor&aacute;rio</p>
<br />
<form>
    <table>
        <tr>
            <td>Disciplina</td>
            <td>Professor</td>
            <td>1º Bimestre</td>
            <td>2º Bimestre</td>
            <td>3º Bimestre</td>
            <td>4º Bimestre</td>
        </tr>
        <tr>

            <td>
                <select id="disciplinaGrade">
                    <option value="<?= @$grade->getDisciplinaCod() ?>"><?= @$disciplinaDesc ?></option>
                    <?php $this->load->view('disciplina/inc/cbo_disciplina'); ?>
                </select>
            </td>
            <td>
                <select id="professorGrade">
                    <option value="<?= @$grade->getProfessorCod() ?>"><?= @$professorNome ?></option>
                    <?php $this->load->view('funcionario/inc/cbo_docente'); ?>
                </select>
            </td>
            <td>
                <input type="text" id="1bim" size="10" value="<?= @$grade->getPrimeiroBim() ?>"/>
            </td>
            <td>
                <input type="text" id="2bim" size="10" value="<?= @$grade->getSegundoBim() ?>" />
            </td>
            <td>
                <input type="text" id="3bim" size="10" value="<?= @$grade->getTerceiroBim() ?>" />
            </td>
            <td>
                <input type="text" id="4bim" size="10" value="<?= @$grade->getQuartoBim() ?>" />
            </td>
        </tr>
        <tr><td colspan="6">&nbsp;</td></tr>
        <tr>
            <td colspan="6">
                <div align="center">
                    <input type="hidden" id="gradeId" value="<?= $grade->getGradeId() ?>" />
                    <input type="hidden" id="turmaId" value="<?= $grade->getTurmaId() ?>" />
                    <input type="button" id="btnAlterarGrade" value="Alterar Hor&aacute;rio" />
                    <input type="button" id="btnRemoverGrade" value="Excluir Hor&aacute;rio" />
                </div>
            </td>
        </tr>
    </table>
  
</form>
</div>
<script>
    
    $('#btnAlterarGrade').click(function(){
        
        var primeiroBim = jQuery('input[id="1bim"]').val();
        var segundoBim = jQuery('input[id="2bim"]').val();
        var terceiroBim = jQuery('input[id="3bim"]').val();
        var quartoBim = jQuery('input[id="4bim"]').val();
        var disciplinaCod = jQuery('select[id="disciplinaGrade"]').val();
        var professorCod = jQuery('select[id="professorGrade"]').val();
        var gradeId = jQuery('input[id="gradeId"]').val();
        var turmaId = jQuery('input[id="turmaId"]').val();
        
        jQuery.ajax( {
            type: "POST",
            url: '?c=c_grade_horaria&m=alteraGradeHoraria',
            cache: false,
            data: {
                primeiroBim:primeiroBim,
                segundoBim:segundoBim,
                terceiroBim:terceiroBim,
                quartoBim:quartoBim,
                disciplinaCod:disciplinaCod,
                professorCod:professorCod,
                gradeId:gradeId
            },
            beforeSend: function (){
                $('#div_body').addClass("div_carregando");
            },
            complete:  function(){
                $('#div_body').removeClass("div_carregando");

            },
            success: function(data){
               
                if(data == true){
                    
                    jQuery.ajax( {
                        type: "POST",
                        url: '?c=c_grade_horaria&m=loadGrade',
                        cache: false,
                        data: {turmaId:turmaId},
                        success: function(data){

                            $('#div_lista_grade').html(data);
                            
                            jQuery(document).trigger('close.facebox');
                        }
                    });
                    
//                    $('#div_lista_grade').append(dados[1]);
                    
                    
                }                    
                else
                    alert('Erro ao gravar. Tente novamente');
            }
        });
        
    });
    
    $('#btnRemoverGrade').click(function(){

        var gradeId = jQuery('input[id="gradeId"]').val();
        
        
        var msgOk = "Deseja realmente excluir a grade?"
        
         if(confirm(msgOk)){
            jQuery.ajax( {
                type: "POST",
                url: '?c=c_grade_horaria&m=excluirGradeHoraria',
                cache: false,
                data: {
                    gradeId:gradeId
                },
                beforeSend: function (){
                    $('#div_body').addClass("div_carregando");
                },
                complete:  function(){
                    $('#div_body').removeClass("div_carregando");

                },
                success: function(data){

                    if(data == true){

                        $('#tr_'+gradeId).remove();

                        jQuery(document).trigger('close.facebox');

                    }                    
                    else
                        alert('Erro ao gravar. Tente novamente');
                }
            });
         }  
    });

</script>